var app = getApp();
const requireHttps = app.globalData.requireHttps //获取require域名
Page({
  data: {
    longitude: '',
    latitude: '',
    garage: [],
    address: '',
    boxShow: true,
    seachPrking: [],
    showParking: true
  },
  onLoad: function(options) {
    let _this = this;
    _this.getClaimGarage()
    _this.setData({
      boxShow: true
    })
  },

  //用户点击选择位置
  setAddress: function() {
    let _this = this;
    //选取地址
    wx.chooseLocation({
      success: res => {
        // console.log(res)
        //获取用户选择地点的经纬度
        let latitude = res.latitude
        let longitude = res.longitude
        //获取用户选择的地址
        _this.setData({
          address: res.address
        })
        //发起网络请求获取用户选择地区车库及距离...
        wx.request({
          url: '' + requireHttps + '/api/garage/getSearchGarage',
          data: {
            lat: latitude,
            lng: longitude,
            now_lat: app.globalData.latitude,
            now_lng: app.globalData.longitude,
            page: 1
          },
          method: 'post',
          success: res => { //请求成功
            console.log(res)
            if (res.data.data == '') {
              _this.setData({
                noParking: '该地区暂无车库',
                showParking: false
              })
            }
            _this.setData({
              seachPrking: res.data.data,
              boxShow: false
            })
          },
          fail: error => { //请求失败
            console.log(error)
            wx.showModal({
              title: '提示',
              content: '请求失败',
              success: res => {
                if (res.confirm) {
                  console.log('用户点击确定')
                } else if (res.cancel) {
                  console.log('用户点击取消')
                }
              }
            })
          }
        })
      },
      fail: error => {
        console.log(error)
      }
    })
  },

  seeCar: e => {
    app.globalData.garageid = e.currentTarget.dataset.garageid
    app.globalData.address = e.currentTarget.dataset.address
    app.globalData.usable = e.currentTarget.dataset.usable
    app.globalData.name = e.currentTarget.dataset.name
    app.globalData.juli = e.currentTarget.dataset.juli
    // console.log(e)
    wx.navigateTo({
      url: '/pages/parkingTab/parkingTab?paringid=paringid',
    })
  },
  //获取认领车库列表
  getClaimGarage: function() {
    wx.showLoading({
      title: '加载中',
      mask: true
    })
    let _this = this;
    wx.request({
      url: '' + requireHttps + '/api/garage/getClaimGarage',
      data: {
        lat: app.globalData.latitude,
        lng: app.globalData.longitude,
        page: 1
      },
      method: 'post',
      success: res => {
        console.log(res);
        console.log(res.data.data.length)
        if (res.data.data.length == 0) {
          _this.setData({
            noParking: '该地区暂无车库',
            showParking: false
          })
        }
        _this.setData({
          garage: res.data.data,
          boxShow: true
        })
        wx.hideLoading()
      }
    })
  },
})